package com.totoro.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.totoro.domain.SysRemarkTemplate;
import com.totoro.service.SysRemarkTemplateService;
import com.totoro.util.CommonConstant;
import com.totoro.util.Page;

@Controller
@RequestMapping("/sysRemarkTemplate")
public class SysRemarkTemplateController {

	@Autowired
	private SysRemarkTemplateService sysRemarkTemplateService;

	@RequestMapping("/list")
	public ModelAndView list(Integer pageNo) {
		System.out.println("/sysRemarkTemplate/list");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/sysRemarkTemplate/list");						  
		
		pageNo = pageNo == null ? 1 : pageNo;
		Page<SysRemarkTemplate> pagedPost = sysRemarkTemplateService.getPagePosts(pageNo,
				CommonConstant.PAGE_SIZE);
		
		mav.addObject("pagedPost", pagedPost);
		mav.addObject("sysRemarkTemplateList", pagedPost.getResult());
		mav.addObject("pageNo", pageNo);
		return mav;
	}

	@RequestMapping("/findByName")
	public ModelAndView findByName(String keyword) {
		System.out.println("description: " + keyword);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("/sysRemarkTemplate/list");
		mav.addObject("sysRemarkTemplateList",
				sysRemarkTemplateService.list("keyword", new String(keyword)));
		return mav;
	}

	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.GET)
	public String toSave(ModelMap modelMap) {	
		modelMap.addAttribute("sysRemarkTemplate",new SysRemarkTemplate());
		return "/sysRemarkTemplate/saveOrUpdate";
	}
//	保存或更新
	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
	public String saveOrUpdate(@ModelAttribute("sysRemarkTemplate") SysRemarkTemplate sysRemarkTemplate) {
		System.out.println("sysRemarkTemplate: " + sysRemarkTemplate);
//		如果id为null，则为save，否则update
		sysRemarkTemplateService.saveOrUpdate(sysRemarkTemplate);
		return "redirect:/sysRemarkTemplate/list.html";
	}

	@RequestMapping("/findById/{id}")
	public String findById(@PathVariable("id") Integer id, ModelMap modelMap) {
		modelMap.addAttribute("sysRemarkTemplate", sysRemarkTemplateService.get(id));
		return "/sysRemarkTemplate/saveOrUpdate";
	}

//	@RequestMapping("/update")
//	public String update(@ModelAttribute("sysRemarkTemplate") SysRemarkTemplate sysRemarkTemplate) {
//		System.out.println(sysRemarkTemplate);
//		sysRemarkTemplateService.update(sysRemarkTemplate);
//		return "redirect:/sysRemarkTemplate/list.html";
//	}

	@RequestMapping("/delete/{id}")
	public String delete(SysRemarkTemplate sysRemarkTemplate) {
		sysRemarkTemplateService.deleteObject(sysRemarkTemplate);
		return "redirect:/sysRemarkTemplate/list.html";
	}
	
	//增加评语模板json视图
	@RequestMapping("/findById")
	public String findByIdToJson(Integer id,ModelMap model){
		System.out.println("id的值为：" + id);
		SysRemarkTemplate sysRemarkTemplate = sysRemarkTemplateService.get(id);
		System.out.println("describtion = " + sysRemarkTemplate.getDescription());
		model.addAttribute("describtion", sysRemarkTemplate.getDescription());
		return "sysRemarkTemplateJson";
	}

}
